Imports Microsoft.VisualBasic
Imports Cdm.DataCore.UserSecurity
Imports System
Imports System.Collections
Imports System.Web.UI
Imports System.Web.UI.WebControls




Public Class GridViewSettingController
    Public Shared Function NumberOfRowsDisplay(ByVal gridViewName As String) As Integer

    End Function

    Public Shared Function SetGridViewValues(ByVal currentDash As System.Web.UI.UserControl, _
                                              ByVal defaultNumberOfRows As Integer, _
                                              ByVal defaultSortExpression As String, _
                                              ByVal defaultSortDirection As WebControls.SortDirection) As Boolean
        Dim iDash As IDashBoard = CType(currentDash, IDashBoard)
        iDash.SortField = GetSortExpression(currentDash, defaultSortExpression)
        iDash.SortDirection = GetSortDirection(currentDash, defaultSortDirection)
        iDash.PageSize = GetNumberOfRows(currentDash, defaultNumberOfRows)

    End Function

    Public Shared Function GetSortExpression(ByVal currentDash As System.Web.UI.UserControl, ByVal defaultValue As String) As String
        Dim gridViewName As String = currentDash.GetType.Name
        If CurrentUserSettings.ContainsKey(gridViewName) Then
            Dim currentSetting As UserGridSetting = CurrentUserSettings(gridViewName)
            Return currentSetting.SortField
        Else
            Return defaultValue
        End If
    End Function

    Public Shared Function GetSortDirection(ByVal currentDash As System.Web.UI.UserControl, ByVal defaultValue As SortDirection) As SortDirection
        Dim gridViewName As String = currentDash.GetType.Name
        If CurrentUserSettings.ContainsKey(gridViewName) Then
            Dim currentSetting As UserGridSetting = CurrentUserSettings(gridViewName)
            If currentSetting.IsAscending Then
                Return WebControls.SortDirection.Ascending
            Else
                Return WebControls.SortDirection.Descending
            End If
        Else
            Return defaultValue
        End If
    End Function

    Public Shared Function GetNumberOfRows(ByVal currentDash As System.Web.UI.UserControl, ByVal defaultValue As Integer) As Integer
        Dim gridViewName As String = currentDash.GetType.Name
        If CurrentUserSettings.ContainsKey(gridViewName) Then
            Dim currentSetting As UserGridSetting = CurrentUserSettings(gridViewName)
            Return currentSetting.NumberOfRows
        Else
            Return defaultValue
        End If
    End Function

    Public Shared Function GetSecondarySetting(ByVal currentDash As System.Web.UI.UserControl, ByVal defaultValue As String) As String
        Dim gridViewName As String = currentDash.GetType.Name
        If CurrentUserSettings.ContainsKey(gridViewName) Then
            Dim currentSetting As UserGridSetting = CurrentUserSettings(gridViewName)
            Return currentSetting.SecondarySetting
        Else
            Return defaultValue
        End If
    End Function


    Private Shared Function CurrentUserSettings() As Hashtable
        If System.Web.HttpContext.Current.Session("GridViewSettings") Is Nothing Then
            Dim grdViewSettings As New Hashtable()
            Dim settings As System.Collections.Generic.IList(Of Cdm.DataCore.UserSecurity.UserGridSetting) = _
                        Cdm.DataCore.UserSecurity.UserGridSetting.ListByUser(UserAuthorization.CurrentUser.UserId)

            For i As Integer = 0 To settings.Count - 1
                If grdViewSettings.ContainsKey(settings(i).GridViewName) Then
                    grdViewSettings.Remove(settings(i).GridViewName)
                End If
                grdViewSettings.Add(settings(i).GridViewName, settings(i))
            Next
            System.Web.HttpContext.Current.Session("GridViewSettings") = grdViewSettings

        End If

        Return System.Web.HttpContext.Current.Session("GridViewSettings")

    End Function

    Public Shared Function SaveSetting(ByVal currentDash As System.Web.UI.UserControl) As Boolean
        Try
            Dim gridViewName As String = currentDash.GetType.Name
            Dim iCurrentDash As IDashBoard = CType(currentDash, IDashBoard)
            Dim newSetting As UserGridSetting
            Dim bExists As Boolean = CurrentUserSettings.ContainsKey(gridViewName)

            If bExists Then
                newSetting = CurrentUserSettings().Item(gridViewName)
            Else
                newSetting = New UserGridSetting()
                newSetting.UserId = UserAuthorization.CurrentUser.UserId
                newSetting.GridViewName = gridViewName
            End If

            newSetting.SortField = iCurrentDash.SortField
            newSetting.IsAscending = (iCurrentDash.SortDirection.Equals(SortDirection.Ascending))
            newSetting.NumberOfRows = iCurrentDash.PageSize
            newSetting.DateModified = Now
            newSetting.LastModified = UserAuthorization.CurrentUser.LogonId
            newSetting.Persist()
            If bExists Then
                CurrentUserSettings.Remove(gridViewName)
            End If

            CurrentUserSettings.Add(gridViewName, newSetting)

        Catch ex As Exception
            Return False
        End Try
    End Function


    Public Shared Function SaveSetting(ByVal currentDash As IDashBoard, ByVal secondarySetting As String) As Boolean
        Try
            Dim newSetting As UserGridSetting
            Dim bExists As Boolean = CurrentUserSettings.ContainsKey(currentDash.Title)

            If bExists Then
                newSetting = CurrentUserSettings().Item(currentDash.Title)
            Else
                newSetting = New UserGridSetting()
                newSetting.UserId = UserAuthorization.CurrentUser.UserId
                newSetting.GridViewName = currentDash.Title
            End If

            newSetting.SortField = currentDash.SortField
            newSetting.IsAscending = (currentDash.SortDirection.Equals(SortDirection.Ascending))
            newSetting.NumberOfRows = currentDash.PageSize
            newSetting.SecondarySetting = secondarySetting
            newSetting.DateModified = Now
            newSetting.LastModified = UserAuthorization.CurrentUser.LogonId
            newSetting.Persist()
            If bExists Then
                CurrentUserSettings.Remove(currentDash.Title)
            End If

            CurrentUserSettings.Add(currentDash.Title, newSetting)

        Catch ex As Exception
            Return False
        End Try
    End Function
End Class
